package org.lzuploader.gui.listeners.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.WMain;
import org.lzuploader.model.Config;

/** This class is used when user selects an account on the main window.
 * 
 * @author Emmanuel Cron
 *
 */
public class AccountSelectionListener implements SelectionListener
{
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(AccountSelectionListener.class);
    
    /** This is used when default action is made on widget.
     * @param e Event that generated this action
     */ 
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);
    }

    /** This is used when action is made on widget.
     * @param e Event that generated this action
     */ 
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("Selection has been made on account list.");
        
        Shell wMainShell = null;
        for (int i = 0 ; i < e.display.getShells().length ; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WMain)
            {
                wMainShell = e.display.getShells()[i];
                break;
            }
        }
        
        final WMain main = (WMain) wMainShell.getData();
        final Config config = Config.getInstance();
        
        log.trace("Saving new selection in config.");
        
        config.setSelectedAccount(main.getAccountsList().getText());
        config.save();
        
        // Refreshing file size display
        main.refreshFileSizeWidgets();
        
        // Refreshing server list
        main.refreshServerList();
    }

}
